﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using System.Xml.Serialization;

namespace AssignmentsManage.DataClasses
{
    [XmlRootAttribute(ElementName = "LinkedFile", IsNullable = false)]
    public class LinkedFile : DependencyObject, INotifyPropertyChanged
    {
        #region Constructors

        public LinkedFile()
        { }

        public LinkedFile(string filePath)
        {
            FilePath = filePath;
        }

        public LinkedFile(string filePath, bool isURL)
            :this(filePath)
        {
            IsURL = isURL;
        }

        #endregion

        #region Properties

        private string _FilePath;
        [XmlElementAttribute("FilePath")]
        public string FilePath
        {
            get
            {
                return _FilePath;
            }
            set
            {
                _FilePath = value;
                OnPropertyChanged("FilePath");
                OnPropertyChanged("FileFound");
                OnPropertyChanged("FileInformation");
            }
        }

        [XmlElementAttribute("IsURL")]
        public bool IsURL
        {
            get;
            set;
        }

        [XmlIgnore()]
        public bool FileFound
        {
            get
            {
                if (!IsURL)
                {
                    return System.IO.File.Exists(_FilePath);
                }
                else
                {
                    return true;
                }
            }
        }

        [XmlIgnore()]
        public string FileName
        {
            get
            {
                if (!IsURL)
                {
                    return new System.IO.FileInfo(FilePath).Name;
                }
                else
                {
                    return FilePath;
                }
            }
        }

        #endregion

        #region Methods



        #endregion

        #region NotifyChanges

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
